<template>
    <div>
        <van-nav-bar
                title="用户登录"
                :fixed="true"
                class="my-nav"
        />
        <div style="margin: 70px 20px">
            <van-cell-group>
                <van-field
                        v-model="username"
                        clearable
                        left-icon="contact"
                        placeholder="用户名/账号"
                        @click-icon="$toast('question')"
                />

                <van-field
                        v-model="password"
                        type="password"
                        placeholder="请输入密码"
                        left-icon="password-not-view"
                        @keyup.enter="login"
                />
            </van-cell-group>
            <van-button type="primary" bottomAction style="margin-top: 20px" @click="login">登录</van-button>
        </div>
        <div style="position: fixed; height: 50px; bottom: 0; width: 100%; background: #fff; z-index: 100"></div>
    </div>
</template>

<script>
  import login from '../../api/login'
  import {Toast} from 'vant'
  import {initCartGoodsNum} from '../../api/carts'
  import {initOrdersNum} from '../../api/order/submit'

  export default {
    data() {
      return {
        username: '',
        password: ''
      }
    },
    methods: {
      login() {
        login(this.username, this.password, data => {
          let redirect = this.$route.query.redirect;
          initCartGoodsNum();
          initOrdersNum();
          if (redirect) {
            this.$router.push({path: redirect})
          }else{
            this.$router.push('home')
          }
        }, e => {
          Toast.fail('登录失败');
        })
      }
    }
  }
</script>

<style lang="less" scoped>

</style>